package com.flyxiaozhu.erp.core.services;

import com.flyxiaozhu.erp.core.entities.GoodsSku;
import com.flyxiaozhu.erp.core.repositories.GoodsSkuRepository;
import com.flyxiaozhu.erp.core.utils.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author flyxiaozhu
 * @date 2019-04-25 14:39
 */
@Service
public class GoodsSkuService {
    @Autowired
    GoodsSkuRepository goodsSkuRepository;

    public GoodsSku findById(long id) {
        return goodsSkuRepository.findById(id).orElse(null);
    }

    public Map<Long, List<GoodsSku>> findByGoodsIdGroup() {
        List<GoodsSku> goodsSkus = goodsSkuRepository.findAll();
        return CollectionUtils.group(goodsSkus, new CollectionUtils.GroupFilter<GoodsSku, Long, GoodsSku>() {
            @Override
            public Long getKey(GoodsSku source) {
                return source.getGoodsId();
            }

            @Override
            public GoodsSku getValue(GoodsSku source) {
                return source;
            }
        });
    }
}
